package com.testingedu.action.background;

import com.testingedu.jdbcDao.TRoleDao;
import com.testingedu.model.TRole;

/**
 * 
 * @author lsww
 * @E-mail lsww90@Gmail.com
 * @createtime 2012-11-21 下午08:54:56
 * @version 1.0
 * @since JDK 1.6
 */
public class TRoleAction extends RequestContext {

	private static final long serialVersionUID = -9146777136466760390L;
	private static TRoleDao tRoleDao = new TRoleDao();
	private TRole tRole;

	public void settRole(TRole tRole) {
		this.tRole = tRole;
	}

	public void _list() throws Exception {
		String url = request.getParameter("url");
		if ("info".equals(url)) {
			this.info(url);
		} else {
			createQueryResult();
			tRole.setTUser(getTUser());
			tRoleDao.list(queryResult, tRole);
			request.setAttribute("queryResult", queryResult);
			request.getRequestDispatcher("/WEB-INF/admin/jsp/role/list.jsp")
					.forward(request, response);
		}
	}

	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			request.getRequestDispatcher("/WEB-INF/admin/jsp/role/add.jsp")
					.forward(request, response);
		} else {
			tRole.setTUser(getTUser());
			int roleId = tRoleDao.add(tRole);
			response.getWriter().write("{\"id\":" + roleId + "}");
			response.getWriter().close();
		}
	}

	public void _delete() throws Exception {
		int id = Integer.parseInt(request.getParameter("id"));
		tRoleDao.delete(id);
		response.sendRedirect("/testingedu2/admin/role/list");
	}

	public void _update() throws Exception {
		String url = request.getParameter("url");
		if ("update".equals(url)) {
			this.info(url);
		} else {
			tRoleDao.update(tRole);
			response.getWriter().write("{\"id\":" + tRole.getId() + "}");
			response.getWriter().close();
		}
	}

	private void info(String url) throws Exception {
		tRole = tRoleDao.info(tRole.getId());
		request.setAttribute("tRole", tRole);
		request.getRequestDispatcher("/WEB-INF/admin/jsp/role/" + url + ".jsp")
				.forward(request, response);
	}
}
